/*
 * Copyright (C) 2011, Pavel Samko <bulldozerbsg@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the "Pavel Samko" nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#ifndef VCMODOPENALOUT_H
#define VCMODOPENALOUT_H

#include "VCStd.h"

#if VC_PLATFORM_WIN
#include <al.h>
#include <alc.h>
#elif VC_PLATFORM_MACOSX
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <AL/al.h>
#include <AL/alc.h>
#endif

#define VCMODOPENALOUT_BUFF_SIZE 3200 // 0.1s
#define VCMODOPENALOUT_BUFF_COUNT 2
#define VCMODOPENALOUT_WAIT_SIZE 32000

class VCModOpenALOut;
class VCModOpenALOutThread: public  VCThread
{
	friend class VCModOpenALOut;
public:
	VCModOpenALOutThread(VCModOpenALOut & mod);
	virtual ~VCModOpenALOutThread() {}

	virtual int Execute(void * data);
	virtual void OnExit(int exitcode);

	typedef enum
	{
		WORK_NONE,
		WORK_INIT,
		WORK_STREAM,
		WORK_STREAM_PLAY,
		WORK_STREAM_FINISHPLAY,
		WORK_ERROR
	} STATE;

	VC_RESULT SetNeedState(STATE needstate);
private:
	VCModOpenALOut & m_mod;

	STATE m_needstate;
	STATE m_state;

	ALCdevice * m_device;
	ALCcontext * m_context;
	ALuint m_source;
	VCMutex m_source_mutex;
	ALuint m_buff[VCMODOPENALOUT_BUFF_COUNT];
	ALuint m_buff_free[VCMODOPENALOUT_BUFF_COUNT];
	ALint m_buff_free_count;
};

class VCModOpenALOut: public VCIMod, public VCIVolume
{
	friend class VCModOpenALOutThread;
public:
	VCModOpenALOut(VCIController & controller);
	virtual ~VCModOpenALOut();

	virtual VC_RESULT Push(VC_CMD cmd, void * data_buff, size_t data_size, VC_DATAFORMAT data_format);

	virtual void SetVolume(unsigned char volume);
	virtual unsigned char GetVolume();
protected:
	bool m_needexit;
	VCModOpenALOutThread * m_thread;

	VCBuffFIFO m_buff;
	VCMutex m_buff_mutex;
};

#endif /* VCMODOPENALOUT_H */

